<?php
namespace app\api\model;

use app\BaseModel;
use think\Db;

class SmsModel extends BaseModel {

	//发送短信验证码
	public function send($phone){	
			$cphone = "86".$phone;
			$checkNum = mt_rand(1000,9999);
			$spid="5535";
			$spsc="00";
			$sppassword="yxjy123";
			$sa="10";
			$da=$cphone;
			$dc="15";
			$sm="您的验证码是：".$checkNum."，10分钟内有效";
			$host="esms100.10690007.net";
			//发送端口，默认80.
			$port=80;
			//拼接URI
			$request = "/sms/mt";
			$request.="?command=MT_REQUEST&spid=".$spid."&spsc=".$spsc."&sppassword=".$sppassword;
			$request.="&sa=".$sa."&da=".$da."&dc=".$dc."&sm=";
			$request.= encodeHexStr($dc,$sm);//下发内容转换HEX编码
			$content = doGetRequest($host,$port,$request);//调用发送方法发送
			
			if(strpos($content,'mterrcode=000')>0){
				Db::name('t_checkcode')->where('phone',$phone)->delete();
				Db::name('t_checkcode')->insert(array("phone"=>$phone,"checkCode"=>$checkNum,"validTime"=>time()+600,"createTime"=>eeGetDateTime()));
				return TRUE;
			}else{
				return FALSE;	
			}
	}
	
	public function checkCode($phone,$code){
		$checkData = Db::name('t_checkcode')->where('phone',$phone)->where('checkCode',$code)->find();
		if(empty($checkData)){
			return FALSE;
		}
		$validTime = $checkData["validTime"];
		if($validTime<time()){
			return FALSE;
		}
		
		Db::name('t_checkcode')->where('phone',$phone)->delete();
		
		return TRUE;
	}	
	
	
	
}